package se.rende.myrss;

import se.rende.myrss.data.MyRSSDataHelper;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ChannelListActivity extends FragmentActivity implements
		ChannelListFragment.Callbacks, View.OnClickListener {

	private ChannelListFragment channelListFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getPrefs();

		setContentView(R.layout.activity_channel_list);

		channelListFragment = (ChannelListFragment) getSupportFragmentManager()
				.findFragmentById(R.id.channel_list);
		channelListFragment.getListView().setOnCreateContextMenuListener(this);
		
		View allButton = findViewById(R.id.all_button);
		allButton.setOnClickListener(this);
	}
	
	private void getPrefs() {
//		SharedPreferences preferences = getPreferences(MODE_PRIVATE);
		// this.showNew = preferences.getBoolean(PREF_SHOW_NEW_ID, true);
	}

//	private void savePrefs() {
//		SharedPreferences preferences = getPreferences(MODE_PRIVATE);
//		SharedPreferences.Editor editor = preferences.edit();
		// editor.putBoolean(PREF_SHOW_NEW_ID, showNew);
//		editor.commit();
//	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.channels, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.addChannel:
			startActivity(new Intent(this, AddChannelActivity.class));
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onItemSelected(String id) {
		Intent intent = new Intent(this, ItemListActivity.class);
		intent.putExtra(ItemListFragment.ARG_CHANNEL_ID, id);
		startActivity(intent);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.channels_context_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		super.onContextItemSelected(item);
		switch (item.getItemId()) {
		case R.id.channel_edit:
			editChannel(info.id);
			return true;
		case R.id.channel_delete:
			deleteChannel(info.id);
			return true;
		default:
			return false;
		}
	}
	
	private void editChannel(long id) {
		Intent intent = new Intent(this, AddChannelActivity.class);
		intent.putExtra(AddChannelActivity.ARG_CHANNEL_ID, "" + id);
		startActivity(intent);
	}

	private void deleteChannel(long id) {
		MyRSSDataHelper dataHelper = new MyRSSDataHelper(this);
		SQLiteDatabase database = dataHelper.getWritableDatabase();
		database.execSQL("delete from channel where _id=?", 
				new String[] {Long.toString(id)});
		database.close();
		channelListFragment.refreshList();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.all_button:
			Intent intent = new Intent(this, ItemListActivity.class);
			intent.putExtra(ItemListFragment.ARG_CHANNEL_ID, "0");
			startActivity(intent);
			break;
		}
	}

}
